package ru.mipt.victator.pagerank.rank;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;

public class SequenceFile2Map
{
	private static void _read(FileSystem FS, Path file, Configuration conf, Map<String,FloatWritable> map) throws IOException
	{
		SequenceFile.Reader reader = new SequenceFile.Reader(FS,file,conf);
		boolean read = true;
		Text text = new Text();
		FloatWritable floatw = new FloatWritable();
		while(read)
		{
			read = reader.next(text, floatw);
			if (read)
			{
				map.put(text.toString(), floatw);
			}
		}
		reader.close();
	}
	
	public static Map<String,FloatWritable> Read(FileSystem FS,Configuration conf,Path files) throws IOException
	{
		Map<String,FloatWritable> res = new TreeMap<String,FloatWritable>();
		
		if (FS.isFile(files))
		{
			_read(FS,files,conf,res);
		}
		else
		{
			Path[] children = FS.listPaths(files);
			for (Path child : children)
			{
				_read(FS,child,conf,res);
			}
		}
		return res;
	}
}
